package tech.runchen.gz5055.oms.modules.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import tech.runchen.gz5055.oms.common.utils.PageInfo;
import tech.runchen.gz5055.oms.common.utils.Query;
import tech.runchen.gz5055.oms.common.utils.StringUtils;
import tech.runchen.gz5055.oms.common.utils.poi.head.ImportProductExcel;
import tech.runchen.gz5055.oms.modules.system.entity.ProductEntity;
import tech.runchen.gz5055.oms.modules.system.mapper.ProductMapper;
import tech.runchen.gz5055.oms.modules.system.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品信息表 服务实现类
 * </p>
 *
 * @author zzj
 * @since 2023-10-10
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductEntity> implements IProductService {

    @Override
    public PageInfo queryPage(Map<String, Object> params) {
        String keyword = (String) params.get("keyword");

        IPage<ProductEntity> page = this.page(
                new Query<ProductEntity>().getPage(params),
                new QueryWrapper<ProductEntity>()
                        .and(!StringUtils.isEmpty(keyword), wrapper ->
                                wrapper.like("product_code", keyword).or().like("product_name", keyword).or().like("model", keyword)
                                        .like("product_name_pinyin", keyword).like("product_name_first_letter", keyword).like("byname",keyword)
                        )
        );

        return new PageInfo(page);
    }

    @Override
    public List<ProductEntity> querySelect(Map<String, Object> params) {
        String keyword = (String) params.get("keyword");

        List<ProductEntity> list = this.list(new QueryWrapper<ProductEntity>().and(!StringUtils.isEmpty(keyword), wrapper ->
                wrapper.like("product_code", keyword).or().like("product_name", keyword).or().like("model", keyword)
                        .like("product_name_pinyin", keyword).like("product_name_first_letter", keyword).like("byname",keyword)
        ));

        return list;
    }

    @Override
    public ProductEntity queryInfoByProductId(String productId) {
        return this.getOne(new QueryWrapper<ProductEntity>().eq("product_id", productId));
    }

    @Override
    public boolean saveProduct(ProductEntity product) {
        product.setProductNamePinyin(PinyinUtil.getPinyin(product.getProductName(), ""));
        product.setProductNameFirstLetter(PinyinUtil.getFirstLetter(product.getProductName(), ""));
        return this.save(product);
    }

    @Override
    public boolean batchImportData(Collection<ImportProductExcel> entityList) {
        for (ImportProductExcel entity : entityList) {
            ProductEntity product = this.queryInfoByProductId(entity.getProductId());
            if (product == null) {
                product = new ProductEntity();
                BeanUtil.copyProperties(entity, product);
                this.saveProduct(product);
            } else {
                BeanUtil.copyProperties(entity, product);
                this.updateProduct(product);
            }
        }
        return true;
    }

    @Override
    public boolean updateProduct(ProductEntity product) {
        product.setProductNamePinyin(PinyinUtil.getPinyin(product.getProductName(), ""));
        product.setProductNameFirstLetter(PinyinUtil.getFirstLetter(product.getProductName(), ""));
        return this.updateById(product);
    }

    @Override
    public void deleteProduct(Long productId) {
        this.removeById(productId);
    }
}
